﻿<?php
@session_start();

require_once("../../class/mysql.class.php");

  $obj = new Mysql();
  
  $obj -> set_table('user_login');
  $obj -> set_colone("(`*`)");
  $obj -> set_variables(" id_login = '" . $_SESSION["id"] . "' ");
  $user = $obj -> select_enregistrement();

	$id_article = addslashes($_REQUEST["ref_article"]); 
	//$type_id = addslashes($_REQUEST["id_type"]; 
	//$emplacement_id = addslashes($_REQUEST["id_emplacement"]; 
	$id_fournisseur = addslashes($_REQUEST["id_fournisseur"]); 
	$quantite = addslashes($_REQUEST["qte"]);  
	$prix = addslashes($_REQUEST["prix"]); 
	//$id_unit_stockage = addslashes($_REQUEST["unit"];
	$id_type_monnaie = addslashes($_REQUEST["id_monnaie"]); 
	$note = addslashes($_REQUEST["comment"]);
	$dateAdd =addslashes($_REQUEST["dateAdd"]);
	$id_user = $user['id_login'];
        
        echo $id_monnaie = $_POST["id_monnaie"];
        echo $prix_en_devise = $_POST["prix_en_devise"];
		
	$req="SELECT Quantite From st_article WHERE id_article=".$id_article;
	$obj = new Mysql ();
	$resultEn = mysql_query($req);
	$rowEn = mysql_fetch_array($resultEn);
	echo $Qte_stock_ancien=$rowEn['Quantite'].'<br>';
	
	$Qte_stock = $Qte_stock_ancien + $quantite; 
	
	echo $Qte_stock.'<br>';
		//add
		$sql = "INSERT INTO `st_attr_entree` (`id_entree`, `id_article`, `id_fournisseur`, `qte`, `prix`,
		`id_devise`, `ajouter_par`, `note`,`date_add`,`last_modif`,`last_user_modif`,`prix_en_devise`)  VALUES 
			('', '".$id_article."', '".$id_fournisseur."', '".$quantite."',
			'".$prix."', '".$id_monnaie."', '".$id_user."', '".$note."','".$dateAdd."','".date('m/d/Y')."','".$id_user."','".$prix_en_devise."');";
			echo $sql;
			
			echo $R_article = "UPDATE `st_article` SET `Quantite`='".$Qte_stock."' WHERE `id_article`='".$id_article."'";
			
			mysql_query($R_article) ;
			
			if(mysql_query($sql)){
				$reqInsert = "UPDATE st_attr_entree SET id_fournisseur='".$id_fournisseur."', prix='".$prix."', id_type='".$id_type_monnaie."',
				last_user_modif='".$id_user."', last_modif='".date('m/d/Y')."', Quantite='".$Qte_stock."' WHERE id_article='".$id_article."'";
				mysql_query($reqInsert);
			}


	    //Test if ref existe than update 
		$rq_test = "select * from st_entree where id_article = ".$id_article;
		echo "<br>-----<br>";
		echo $rq_test;
		echo "<br>-----<br>";
		$res = mysql_query($rq_test);
		$vartest = null;
		while ($d = mysql_fetch_array($res)) {
			$vartest .= $d['id_article'];
		}
		//nouvelle entree
		if($vartest==null){
			$s = "INSERT INTO `st_entree` (`id`,`id_article`, `date`) VALUES ('','".$id_article."', '".date('d/m/Y H:i:s')."');";
				echo $s;
				mysql_query($s);
		}
		// update existant avec la 
		else{
			$reqUpdate = "UPDATE st_entree SET date='".date('d/m/Y H:i:s')."' WHERE id_article='".$id_article."'";
			echo "<br>-----<br>";
			echo $reqUpdate;
			echo "<br>-----<br>";
			mysql_query($reqUpdate);
		}

		
header('location:../../list_entree.php?action=added&page=list_entree');
	

